<script setup>
    import * as cpzzApi from '@/api/cpzzApi' 
    import { message } from 'heyui';
    const params = ref({});
    let UserParam = window.external.UserParams?window.external.UserParams:"";  
    let originParam = {};
    let obj = UserParam.split("&");
    for(var i = 0;i<obj.length;i++){
        originParam[obj[i].split("=")[0]] = obj[i].split("=")[1]
    }
    params.value = originParam;
    
    const loading = ref(false);
    const deletable = ref(false);
    const publishStatus = ref("all");
    const publishStatusData = ref([
        {title:'全部',key:'all'},
        {title:'发布成功',key:'0'},
        {title:'发布失败',key:'-1'}
    ])
    const name = ref("");
    const refTable = ref()
    const datas = ref([]);
 
    let checkData = [];
    const select = (data)=>{
        checkData = [];
        if(data.length>0){
            data.map(item=>{
                checkData.push(item.pubId);
            })
        }
    }
    //选中发布
    const checkPublish = ()=>{
        publish(checkData);
     }
     //失败发布
     const errorPublish = ()=>{
        let publishIds = [];
        datas.value.map(item=>{
            item.pubStatus == -1 && publishIds.push(item.pubId);
        })
        publish(publishIds);
    }
    //全部发布
    const allPublish = ()=>{
        let publishIds = [];
        datas.value.map(item=>{
            publishIds.push(item.pubId);
        })
        publish(publishIds);
    }
    const publishSingle = (data)=>{
        publish([data.pubId]);
    }
    const publish = (pubIdsArr)=>{
        if(pubIdsArr.length == 0){
            alert("请至少选择一个发布");
            //message({type:'warning',text: `请至少选择一个发布`});
            return;
        }
        loading.value = true;
        cpzzApi.publish({
            taskId:params.value.taskId?params.value.taskId:'1',
            pubIds:pubIdsArr.join(","),
            userId:params.value.userId?params.value.userId:'1',
            year:params.value.year?params.value.year:'2024',
            orders:params.value.orders?params.value.orders:'1',
            areaIds:params.value.areaIds?params.value.areaIds:'',
            modelId:params.value.modelId?params.value.modelId:'1'
        }).then(res=>{
            loading.value = false;
            const {data,code} = res;
            if(data){
                getPublishList();
                alert("发布成功")
                //message({type:'success',text: `发布成功`});
            }else{
                alert("发布失败")
                //message({type:'error',text: `发布失败`});
            }
        })
    }
    const getPublishList = ()=>{
        loading.value = true;
        checkData = [];
        cpzzApi.findPubTypeListByParams({
            taskId:params.value.taskId?params.value.taskId:'1',
            pubStatus:publishStatus.value == "all"?"":publishStatus.value,
            pubObj:name.value
        }).then(res=>{
            loading.value = false;
            const {data,code} = res;
            if(data.length>0 && code == 200){
                datas.value = data;
            }else{
                datas.value = [];
            }
        })
    }
    onMounted(()=>{
        getPublishList();
    })
</script>

<template>
<div class="publishContain">
    <div class="publishBtns">
        <Button color="blue" class="h-btn h-btn-s" style="margin:0px 10px" @click="allPublish">全部发布</Button>
        <Button color="blue" class="h-btn h-btn-s" style="margin:0px 10px" @click="checkPublish">选中发布</Button>
        <Button color="blue" class="h-btn h-btn-s" style="margin:0px 10px" @click="errorPublish">失败重发</Button>
    </div>
    <div class="publishContents">
        <div class="publishQuery">
            <div>
                <span>发布对象：</span>
                <Input type="text" v-model="name" />
            </div>
            <div>
                <span>发布状态：</span>
                <Select v-model="publishStatus" :datas="publishStatusData" :deletable="deletable" :autosize="deletable"></Select>
            </div>
            <div class="btns">
                <Button color="blue" class="h-btn h-btn-s" style="margin:0px 10px" @click="getPublishList">刷新表格</Button>
            </div>
        </div>
        <div class="publishTable">
            <Table :datas="datas" checkbox :loading="loading" :ref="refTable" @select="select">
                <TableItem title="序号" width="50px" align="center"><template #default="{ index }">{{ index }}</template></TableItem>
                <TableItem title="发布对象" width="100px" prop="pubObj" align="center"/>
                <TableItem title="发布方式" width="100px" prop="configName" align="center"/>
                <TableItem title="发布地址" prop="pubSub" align="center"/>
                <TableItem title="所属组" width="150px" prop="pubGroup" align="center"/>
                <TableItem title="发布时间" width="150px" prop="pubTime" align="center"/>
                <TableItem title="发布人"  width="100px" prop="pubUser" align="center"/>
                <TableItem title="发布状态" align="center" width="70px">
                    <template #default="{ data }">
                        <span>{{data.pubStatus == '0'?'发布成功':data.pubStatus == '-1'?'发布失败':''}}</span>
                    </template>
                </TableItem>
                <TableItem title="发布" align="center" width="70px">
                    <template #default="{ data }">
                        <Button color="blue" class="h-btn h-btn-s" style="margin:0px 10px" @click="publishSingle(data)">发布</Button>
                    </template>
                </TableItem>
            </Table>
        </div>
    </div>
</div>
</template>

<style scoped  lang="scss">
    .publishContain{
        width: 100%;
        height: 100%;
        color: #fff;
        .publishBtns{
            height: 50px;
            line-height: 50px;
            border-bottom:2px solid #085898;
        }
        .publishContents{
            margin-top: 10px;
            width: 100%;
            padding:10px;
            height: calc(100% - 60px);
            border:2px solid #085898;
        }
        .publishQuery{
            height: 30px;
            line-height: 30px;
            display: flex;
            gap: 10px;
            position: relative;
            >div{
                display: flex;
            }
            .btns{
                position: absolute;
                right: 0px;
                top: 0px;
            }
        }
        .publishTable{
            width: 100%;
            margin-top: 10px;
            height: calc(100% - 50px);
        }
    }
</style>
<style lang="scss">
.publishContain{
    .h-select-input-border .h-select-show{
        background-color:transparent;
        min-height: 30px;
    }
    .h-select-value-single{
        line-height: 25px;
        height: 25px;
        min-width: 90px;
    }
    input.h-input{
        background: transparent;
        color: #fff;
        text-align: left;
        height:30px;
    }
    .h-table-header{
        background-color: transparent;
    }
    .h-table .h-table-th{
        background-color: transparent;
        padding:0px;
    }
    .h-table-tbody,.h-table .h-table-tbody tr.h-table-tr-hovered{
        background: #021d45;
        background-color: #021d45;
    }
    .h-table td{
        padding: 0px;
        height: 40px;
    }
}
</style>